/*
 * @Author: new-wang
 * @Date: 2025-08-27 09:36:30
 * @LastEditors: new-wang
 * @LastEditTime: 2025-09-03 15:14:26
 * @FilePath: \nest-demo\src\entityAll\entities\sys-user.entity.ts
 * @Description: 用户实体 - 对应用户核心身份表
 */
import { DelFlagEnum } from '../../common/enum'
import {
  Entity,
  Column,
  Index,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  UpdateDateColumn,
  BaseEntity,
} from 'typeorm'

@Index('fk_user_organization_id', ['organizationId'], {})
@Index('fk_user_system_id', ['systemId'], {})
@Index('idx_username', ['username'], {})
@Index('idx_uuid', ['uuid'], { unique: true })
@Index('username', ['username'], { unique: true })
@Index('uuid', ['uuid'], { unique: true })
@Entity('users', {
  schema: 'nest-demo',
  comment: '系统用户表',
})
export class UserEntity extends BaseEntity {
  @PrimaryGeneratedColumn({
    type: 'bigint',
    name: 'id',
    comment: '使用BIGINT应对海量用户,主键，自增ID，唯一标识',
    unsigned: true,
  })
  id: number

  @Column('char', {
    name: 'uuid',
    unique: true,
    comment: '对外暴露的UUID，避免爬虫和枚举',
    length: 36,
  })
  uuid: string

  @Column('varchar', {
    name: 'username',
    unique: true,
    comment: '用户登录名；\r\n后期 ： 允许为NULL，允许用户仅用邮箱登录',
    length: 50,
  })
  username: string

  @Column('varchar', {
    name: 'email',
    nullable: true,
    comment: '邮箱，唯一，可用于登录或找回密码',
    length: 100,
  })
  email: string | null

  @Column('timestamp', {
    name: 'email_verified_at',
    nullable: true,
    comment:
      '邮箱验证时间，NULL表示未验证；\r\n用于实现邮箱验证功能，是许多系统的基础要求；',
  })
  emailVerifiedAt: Date | null

  @Column('varchar', {
    name: 'phone',
    nullable: true,
    comment: '手机号',
    length: 20,
  })
  phone: string | null

  @Column('varchar', {
    name: 'password_hash',
    comment:
      '加密后的密码哈希值；\r\n应该存储通过 bcrypt、Argon2 等专业算法加密后的哈希值。长度 255 是为了兼容所有哈希算法',
    length: 255,
  })
  passwordHash: string

  @Column('tinyint', {
    name: 'status',
    comment: ' 管理用户账户状态，非常必要（例如：激活、禁用、软删除标记）',
    width: 1,
    default: () => "'1'",
  })
  status: boolean | string

  @Column('tinyint', {
    name: 'del_flag',
    comment: ' 管理用户删除状态，（0 存在，1 删除）',
    width: 1,
    default: DelFlagEnum.NORMAL,
  })
  delFlag: DelFlagEnum

  @Column('varchar', {
    name: 'remember_token',
    nullable: true,
    comment: '“记住我”功能的令牌',
    length: 100,
  })
  rememberToken: string | null

  @Column('int', {
    name: 'system_id',
    nullable: true,
    comment: '所属系统ID',
    unsigned: true,
  })
  systemId: number | null

  @Column('int', {
    name: 'organization_id',
    nullable: true,
    comment: '所属组织ID',
    unsigned: true,
  })
  organizationId: number | null

  @Column('timestamp', {
    name: 'created_at',
    nullable: true,
    comment: '创建时间\r\n',
    default: () => 'CURRENT_TIMESTAMP',
  })
  createdAt: Date | null

  @Column('timestamp', {
    name: 'updated_at',
    nullable: true,
    comment: '更新时间',
    default: () => 'CURRENT_TIMESTAMP',
  })
  updatedAt: Date | null

  @Column('timestamp', {
    name: 'last_login_time',
    nullable: true,
    comment: '最后登录时间',
  })
  lastLoginTime: Date | null

  @Column('varchar', {
    name: 'last_login_ip',
    nullable: true,
    comment: '最后登录ip',
    length: 128,
    default: '',
  })
  lastLoginIp: string

  // 对应关系先不加
  // @OneToMany(() => Roles, (roles) => roles.createBy2)
  // roles: Roles[];

  // @OneToMany(() => Roles, (roles) => roles.updateBy2)
  // roles2: Roles[];

  // @OneToOne(() => UserProfiles, (userProfiles) => userProfiles.user)
  // userProfiles: UserProfiles;

  // @OneToMany(() => UserRoles, (userRoles) => userRoles.user)
  // userRoles: UserRoles[];

  // @ManyToOne(() => Organizations, (organizations) => organizations.users, {
  //   onDelete: "CASCADE",
  //   onUpdate: "RESTRICT",
  // })
  // @JoinColumn([{ name: "organization_id", referencedColumnName: "id" }])
  // organization: Organizations;

  // @ManyToOne(() => Systems, (systems) => systems.users, {
  //   onDelete: "CASCADE",
  //   onUpdate: "RESTRICT",
  // })
  // @JoinColumn([{ name: "system_id", referencedColumnName: "id" }])
  // system: Systems;
}
